/*******************************************************************************
 * Copyright (c) 2013, 2025 Lablicate GmbH.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 * Philip Wenig - initial API and implementation
 *******************************************************************************/
package org.eclipse.chemclipse.support.ui.preferences.fieldeditors;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

public class TextFieldEditor extends FieldEditor {

	private Text text;

	public TextFieldEditor(String name, String labelText, Composite parent) {

		init(name, labelText);
		createControl(parent);
	}

	@Override
	protected void adjustForNumColumns(int numColumns) {

		if(numColumns >= 2) {
			GridData gridData = (GridData)text.getLayoutData();
			gridData.horizontalSpan = numColumns - 1;
			gridData.grabExcessHorizontalSpace = true;
		}
	}

	@Override
	protected void doFillIntoGrid(Composite parent, int numColumns) {

		getLabelControl(parent);
		text = new Text(parent, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);

		GridData gridData = new GridData();
		gridData.widthHint = convertHorizontalDLUsToPixels(text, 4 * IDialogConstants.ENTRY_FIELD_WIDTH);
		gridData.heightHint = convertHorizontalDLUsToPixels(text, 2 * IDialogConstants.ENTRY_FIELD_WIDTH);
		text.setLayoutData(gridData);
	}

	@Override
	protected void doLoad() {

		text.setText(getPreferenceStore().getString(getPreferenceName()));
	}

	@Override
	protected void doLoadDefault() {

		text.setSelection(getPreferenceStore().getDefaultInt(getPreferenceName()));
	}

	@Override
	protected void doStore() {

		getPreferenceStore().setValue(getPreferenceName(), text.getText().trim());
	}

	@Override
	public int getNumberOfControls() {

		return 2;
	}
}